package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.ClientLost;
import cn.wolfcode.crm.query.ClientLostQueryObject;
import cn.wolfcode.crm.service.IClientLostService;
import cn.wolfcode.crm.util.JsonResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("clientlost")
public class ClientLostController {
    @Autowired
    private IClientLostService clientLostService;

    @RequiresPermissions(value = {"clientLost:view", "学员流失视图"}, logical = Logical.OR)
    @RequestMapping("view")
    public String view() {
        return "clientlost/clientlost";
    }


    @RequiresPermissions(value = {"clientLost:query", "学员流失列表"}, logical = Logical.OR)
    @RequestMapping("query")
    @ResponseBody
    public PageResult query(ClientLostQueryObject qo) {
        return clientLostService.query(qo);
    }


    @RequiresPermissions(value = {"clientLost:saveOrUpdate", "学员流失编辑/新增"}, logical = Logical.OR)
    @RequestMapping("saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(ClientLost entity) {
        try {
            clientLostService.updateByPrimaryKey(entity);
            return new JsonResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult().mark("操作失败!原因:" + e.getMessage());
        }
    }


    @RequestMapping("changState")
    @ResponseBody
    public JsonResult changState(Long id, Long client_id) {
        System.out.println(client_id);
        JsonResult result = new JsonResult();
        try {
            clientLostService.changState(id, client_id);
        } catch (Exception e) {
            result.mark(e.getMessage());
            return result;
        }
        return result;
    }


    /*@RequiresPermissions(value={"clientLost:selectAll","学员流失全选"},logical = Logical.OR)
    @RequestMapping("selectAll")
    @ResponseBody
    public List<ClientLost> selectAll() {
        return clientLostService.selectAll();
    }
*/

    @RequestMapping("exportXls")
    @ResponseBody
    public void exportXls(HttpServletResponse response, ClientLostQueryObject qo) throws Exception {
        //设置文件下载响应头

        response.setHeader("Content-Disposition", "attachment;filename=ClientLost.xls");

        Workbook book = clientLostService.exportXls(qo);

        //写入数据输出到浏览器
        book.write(response.getOutputStream());
    }

    @RequestMapping("leave")
    @ResponseBody
    public JsonResult leave() throws Exception {
        JsonResult result = new JsonResult();
        return result;

    }


}
